package com.shen.util;

import com.shen.pojo.Student;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Author:shen
 * CreateDate:2020/7/2.
 * Function:
 */
//数据库连接处理
public class BaseDao {
    private static String username;
    private static String password;
    private static String driver;
    private static String url;
    private static final String PATH = "properties.properties";
    static {
        init();
    }
    private static void init(){
        Properties properties=new Properties();
        InputStream inputStream=BaseDao.class.getClassLoader().getResourceAsStream(PATH);
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        username=properties.getProperty("username");
        password=properties.getProperty("password");
        driver=properties.getProperty("driver");
        url=properties.getProperty("url");
    }
    Connection connection=null;
    PreparedStatement preparedStatement=null;
    ResultSet resultSet=null;

    /**
     * 建立连接
     * @return
     */
    public Connection getConnection(){
        try {
             Class.forName(driver);
            connection= DriverManager.getConnection(url,username,password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    /**
     * 关闭所有
     * @param connection
     * @param preparedStatement
     * @param resultSet
     */
    public void closeAll(Connection connection,PreparedStatement preparedStatement,ResultSet resultSet){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(preparedStatement!=null){
            try {
                preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 增删改通用方法
     * @param sql sql语句
     * @param param sql语句中传递的参数
     * @return
     */
    public  int executeUpdate(String sql,Object... param){
        connection = getConnection();
        int num=0;//返回的行数
        try {
            preparedStatement=connection.prepareStatement(sql);
            for (int i = 0; i < param.length; i++) {
                preparedStatement.setObject(i+1,param[i]);
            }
            num = preparedStatement.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(connection,preparedStatement,resultSet);
        }
        return num;
    }

    /**
     * 返回从数据库中读取到的类
     * @param resultSet  结果集
     * @param tClass  要赋值的类
     * @param <T>  返回一个泛型的对象
     * @return
     * @throws IllegalAccessException
     * @throws InstantiationException
     * @throws SQLException
     * @throws IntrospectionException
     * @throws InvocationTargetException
     */
   private  <T> T getObject(ResultSet resultSet,Class<T> tClass) throws IllegalAccessException, InstantiationException, SQLException, IntrospectionException, InvocationTargetException, NoSuchFieldException {
       T object = tClass.newInstance();
       Class<?> aClass = object.getClass();
       //用于获取列信息
       ResultSetMetaData metaData = resultSet.getMetaData();
       for (int i = 0; i <metaData.getColumnCount() ; i++) {
           //获得列名
           String colName = metaData.getColumnName(i + 1);
           Field declaredField = aClass.getDeclaredField(colName);
           declaredField.setAccessible(true);
           declaredField.set(object,resultSet.getObject(colName));
       }
       return object;
   }

    /**
     *单条查询
     * @param sql  sql语句
     * @param objects  传入sql语句的参数
     * @param tClass  要操作的对象
     * @param <T> 返回的对象
     * @return
     */
    public <T> T getSingleResult(String sql,Object[] objects,Class<T> tClass){
        connection = getConnection();
        T obj=null;
        try {
            preparedStatement=connection.prepareStatement(sql);
            if(objects!=null){
                for (int i = 0; i < objects.length; i++) {
                    preparedStatement.setObject((i+1),objects[i]);
                }
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                try {
                    obj=getObject(resultSet,tClass);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InstantiationException e) {
                    e.printStackTrace();
                } catch (IntrospectionException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(connection,preparedStatement,resultSet);
        }
        return  obj;
    }

    /**
     * 返回多条数据
     * @param sql
     * @param objects
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> List<T> getComplexResult(String sql, Object[] objects, Class<T> tClass){
        connection = getConnection();
        List<T> tList=new ArrayList<>();
        try {
            preparedStatement=connection.prepareStatement(sql);
            if(objects!=null){
                for (int i = 0; i < objects.length; i++) {
                    preparedStatement.setObject((i+1),objects[i]);
                }
            }
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet!=null){
                while (resultSet.next()){
                    T obj=getObject(resultSet,tClass);
                    tList.add(obj);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } finally {
            closeAll(connection,preparedStatement,resultSet);
        }
        return tList;
    }
}
